package com.platform.service;

import com.alibaba.fastjson.JSONObject;
import com.platform.dao.*;
import com.platform.entity.*;
import com.platform.util.CommonUtil;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ApiUserExchangeService {
    @Autowired
    private ApiExchangeCodeMapper exchangeCodeMapper;
    @Autowired
    private ApiAddressMapper apiAddressMapper;
    @Autowired
    private ApiOrderMapper apiOrderMapper;
    @Autowired
    private ApiUserExchangeMapper apiUserExchangeMapper;
    @Autowired
    private ApiExchangeCodeMapper apiExchangeCodeMapper;
    @Autowired
    private ApiOrderGoodsMapper apiOrderGoodsMapper;
    @Autowired
    private ApiGiftTypeMapper apiGiftTypeMapper;

    public Map<String, Object> verify(JSONObject jsonParam){
        Map<String, Object> resultObj = new HashMap<String, Object>();

        Map<String,Object> map = new HashMap<>();
        map.put("giftTypeId",jsonParam.getInteger("giftTypeId"));
        map.put("cardNumber",jsonParam.getString("cardNumber"));
        map.put("exchangeCode",jsonParam.getString("exchangeCode"));
        ExchangeCode exchangeCode =exchangeCodeMapper.queryObject(map);
        if (null== exchangeCode){
            resultObj.put("errno", 400);
            resultObj.put("errmsg", "卡号或兑换码错误");
            return resultObj;
        }
        if (exchangeCode.getIsUse()==2){
            resultObj.put("errno", 401);
            resultObj.put("errmsg", "兑换码已被使用");
            return resultObj;
        }
        if (jsonParam.getInteger("giftTypeId")!=exchangeCode.getGiftTypeId()){
            resultObj.put("errno", 402);
            resultObj.put("errmsg", "礼包类型和卡号兑换码不匹配");
            return resultObj;
        }
        resultObj.put("errno", 200);
        resultObj.put("errmsg", "卡号兑换码验证成功");
        return resultObj;
    }
    /*
     * 兑换码兑换
     * */
    @Transactional
    public Map<String, Object> queryObject(JSONObject jsonParam, UserVo loginUser){
        Map<String, Object> resultObj = new HashMap<String, Object>();

        Map<String,Object> map = new HashMap<>();
        map.put("cardNumber",jsonParam.getString("cardNumber"));
        map.put("exchangeCode",jsonParam.getString("exchangeCode"));
        OrderVo orderInfo = new OrderVo();
        //查询卡号和兑换码是否存在
        ExchangeCode exchangeCode =exchangeCodeMapper.queryObject(map);
        if (null== exchangeCode){
            resultObj.put("errno", 400);
            resultObj.put("errmsg", "卡号或兑换码错误");
            return resultObj;
        }
        //生成订单
        String orderSn =CommonUtil.generateOrderNumber();
        String userId=jsonParam.getString("userId");
        orderInfo.setOrder_sn(orderSn);
        orderInfo.setUser_id(Long.valueOf(userId));
        //查询地址
        AddressVo addressVo=null;
        String addressId=jsonParam.getString("addressId");
        if (StringUtils.isNotEmpty(addressId)){
            addressVo = apiAddressMapper.queryObject(Integer.valueOf(addressId));
        }
        if (addressVo!=null){
            orderInfo.setConsignee(addressVo.getUserName());
            orderInfo.setMobile(addressVo.getTelNumber());
            orderInfo.setCountry(addressVo.getNationalCode());
            orderInfo.setProvince(addressVo.getProvinceName());
            orderInfo.setCity(addressVo.getCityName());
            orderInfo.setDistrict(addressVo.getCountyName());
            orderInfo.setAddress(addressVo.getDetailInfo());
        }
        //下单来源: 0:默认进小程序直接下的单 1:扫酒店二维码下的单' 2:兑换码生成的订单;
        orderInfo.setSource(2);
        orderInfo.setAdd_time(new Date());
        orderInfo.setGoods_price(new BigDecimal(0.0));
        orderInfo.setOrder_price(new BigDecimal(0.0));
        orderInfo.setActual_price(new BigDecimal(0.0));
        //订单状态: 已付款
        orderInfo.setOrder_status(201);
        orderInfo.setShipping_status(0);
        //支付状态：已付款
        orderInfo.setPay_status(2);
        orderInfo.setShipping_id(0);
        orderInfo.setShipping_fee(new BigDecimal(0));
        orderInfo.setIntegral(0);
        orderInfo.setIntegral_money(new BigDecimal(0));
        orderInfo.setFinalPayTime(new Date());
        orderInfo.setFreight_price(0);
        //保存至订单表
        apiOrderMapper.save(orderInfo);
        //保存至用户兑换码表
        UserExchange userExchange = new UserExchange();
        userExchange.setUserId(loginUser.getUserId());
        userExchange.setExchangeCodeId(exchangeCode.getId());
        userExchange.setExchangeTime(new Date());
        userExchange.setOrderSn(orderSn);
        apiUserExchangeMapper.save(userExchange);
        //修改兑换码的状态 1:未使用 2:已使用
        ExchangeCode ex=new ExchangeCode();
        ex.setIsUse(2);
        ex.setId(exchangeCode.getId());
        apiExchangeCodeMapper.update(ex);
        //插入订单商品表
        GiftType giftType =apiGiftTypeMapper.queryObject(exchangeCode.getGiftTypeId());
        OrderGoodsVo orderGoodsVo = new OrderGoodsVo();
        orderGoodsVo.setOrder_id(orderInfo.getId());
        orderGoodsVo.setGoods_id(0);
        orderGoodsVo.setGoods_sn(null);
        orderGoodsVo.setProduct_id(0);
        orderGoodsVo.setGoods_name(giftType.getGiftName());
        orderGoodsVo.setList_pic_url(giftType.getGiftPicUrl());
        orderGoodsVo.setMarket_price(new BigDecimal(0.0));
        orderGoodsVo.setRetail_price(new BigDecimal(0.0));
        orderGoodsVo.setNumber(1);
        orderGoodsVo.setGoods_specifition_name_value(giftType.getGiftContain());
        orderGoodsVo.setGoods_specifition_ids(null);
        apiOrderGoodsMapper.save(orderGoodsVo);

        resultObj.put("errno", 200);
        resultObj.put("errmsg", "订单提交成功");
        return resultObj;
    }


}

